<?php

function input_csv($handle) {
  $out = array();
  $n = 0;
  while ($data = fgetcsv($handle, 10000)) {
    $num = count($data);
    for ($i = 0; $i < $num; $i++) {
      $out[$n][$i] = $data[$i];
    }
    $n++;
  }
  return $out;
}

function uploadFile($files,$dirname) {
  $upload = new \Think\Upload(); // 实例化上传类
  $upload->maxSize = 3145728; // 设置附件上传大小
  $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
  $upload->rootPath = dirname(__FILE__) . "/../../../Uploads/$dirname/"; // 设置附件上传根目录
  $upload->savePath = ''; // 设置附件上传（子）目录
  $upload->subName = array('date', 'Ymd');
  // 上传文件 
  $info = $upload->uploadOne($files);
  if (!$info) { // 上传错误提示错误信息
    return '';
  } else { // 上传成功 获取上传文件信息
    return "Uploads/$dirname/" . $info['savepath'] . $info['savename'];
  }
}

function i_i_array_column($input, $columnKey, $indexKey=null){
  if(!function_exists('i_array_column')){
    $columnKeyIsNumber  = (is_numeric($columnKey))?true:false;
    $indexKeyIsNull            = (is_null($indexKey))?true :false;
    $indexKeyIsNumber     = (is_numeric($indexKey))?true:false;
    $result                         = array();
    foreach((array)$input as $key=>$row){
      if($columnKeyIsNumber){
        $tmp= array_slice($row, $columnKey, 1);
        $tmp= (is_array($tmp) && !empty($tmp))?current($tmp):null;
      }else{
        $tmp= isset($row[$columnKey])?$row[$columnKey]:null;
      }
      if(!$indexKeyIsNull){
        if($indexKeyIsNumber){
          $key = array_slice($row, $indexKey, 1);
          $key = (is_array($key) && !empty($key))?current($key):null;
          $key = is_null($key)?0:$key;
        }else{
          $key = isset($row[$indexKey])?$row[$indexKey]:0;
        }
      }
      $result[$key] = $tmp;
    }
    return $result;
  }else{
    return i_array_column($input, $columnKey, $indexKey);
  }
}


function add_log($user,$money,$amount, $type,$msg,$huoyue = 0){
	$data['user_id'] = $user;
	$data['money'] = $money;
	$data['amount'] = $amount;
	$data['huoyue'] = ($type==27)?$huoyue:0;
	$data['huoyue_jc'] = ($type==28)?$huoyue:0;
	$data['type'] = $type;
	$data['msg'] = $msg;
	$data['date'] = time();
	M("Log")->add($data);
}

function change_username($user_name){
  $array = explode('@', $user_name);
  $username = $array[0];
	if(strlen($username)>3){
		$first = substr($username, 0, 2);
		$last = substr($username, -2);
		$username = $first."***".$last."@".$array[1];
	}else{
		$username = $user_name;
	}
  return $username;
}

//检查是否是下线
function checkchild($uid,$parent){
	$is_parent = 0;
	$child = $uid;

	while($is_parent == 0){
		$pid = M("User")->where(array("id"=>$child))->getField("parent");
		if(!$pid){
			$is_parent = 0;
			return $is_parent;
		}else{
			if($pid == $parent){
				$is_parent = 1;
				return $is_parent;
			}else{
				$child = $pid;
			}
		}
	}
}

//ypp 数组内元素相加（精确小数）
function ypp_array_sum($arr)
{
	$a = 0;
	foreach($arr as $k=>$v){
		$a = bcadd($v,$a,8);
	}
	return  sctonum($a);
}

/*小数处理*/
function sctonum($num){
	$x=explode(".",$num);
	$x1 = substr($x[1], 0, 1);
	$x2 = substr($x[1], 1, 1);
	$x3 = substr($x[1], 2, 1);
	$x4 = substr($x[1], 3, 1);
	$x5 = substr($x[1], 4, 1);
	$x6 = substr($x[1], 5, 1);
	$x7 = substr($x[1], 6, 1);
	$x8 = substr($x[1], 7, 1);
	if($x1 > 0){
		$double = 1;
	}
	if($x2 > 0){
		$double = 2;
	}
	if($x3 > 0){
		$double = 3;
	}
	if($x4 > 0){
		$double = 4;
	}
	if($x5 > 0){
		$double = 5;
	}
	if($x6 > 0){
		$double = 6;
	}
	if($x7 > 0){
		$double = 7;
	}
	if($x8 > 0){
		$double = 8;
	}
	$num = floatval($num);
	if(false !== stripos($num, "e")){
		$a = explode("e",strtolower($num));
		return bcmul($a[0], bcpow(10, $a[1], $double), $double);
	}else{
		return  $num;
	}
}

/* mai start 添加对类的声明调用 */
function CoinClient($username, $password, $ip, $port, $timeout = 3, $headers = array(), $suppress_errors = false)
{
	return new \Think\CoinClient($username, $password, $ip, $port, $timeout, $headers, $suppress_errors);
}

function debug($value, $type = 'DEBUG', $verbose = false, $encoding = 'UTF-8')
{
	if (M_DEBUG || MSCODE) {
		if (!IS_CLI) {
			Think\FirePHP::getInstance(true)->log($value, $type);
		}
	}
}
/* mai end 添加对类的声明调用 */

/* mai start 对接莱特币接口 */

/* 创建钱包地址 */
function createnewaccount($username)
{
	/*$dj_username = 'litecoin';
	$dj_password = '123456';
	$dj_address = '192.168.0.166';
	$dj_port = '40001';*/

	$dj_username = M("Ltset")->where(array('id'=>1))->getField("lt_username");
	$dj_password = M("Ltset")->where(array('id'=>1))->getField("lt_password");
	$dj_address = M("Ltset")->where(array('id'=>1))->getField("lt_address");
	$dj_port =  M("Ltset")->where(array('id'=>1))->getField("lt_port");

	$CoinClient = CoinClient($dj_username, $dj_password, $dj_address, $dj_port, 5, array(), 1);

	$json = $CoinClient->getinfo();//获取钱包信息


	if(isset($json['version']) && !empty($json['version'])) {
		$qianbao_addr = $CoinClient->getaddressesbyaccount($username);//根据用户名查询已生成钱包地址,如果查询到了,返回该地址.

		if (is_array($qianbao_addr)) {
			$qianbao = $qianbao_addr[0]; //已生成的钱包地址
		}
		else {
			$qianbao_ad = $CoinClient->getnewaddress($username);
			//根据用户名生成钱包地址,13800138000为你们系统的登录用户账号或其他确定用户唯一性的参数,不建议使用id

			
			if ($qianbao_ad) {
				$qianbao = $qianbao_ad; //新生成的钱包地址
			}
			else {
				$res['error'] = 1 ;
				$res['msg'] = '新生成钱包地址出错！'; 
				return $res;
			}
		}
	
		if ($qianbao) {
			
			/* $rs = M('User')->where(array('username' => $username))->save(array('qbdz' => $qianbao)); //把地址保存到数据库
			if (!$rs) {
				$res['error'] = 1 ;
				$res['msg'] = '保存钱包地址出错！'; 
				return $res;
			}
			else
			{
				$res['error'] = 0 ;
				$res['qbdz'] = $qianbao ; //成功
				return $res;
			} */
			$res['error'] = 0 ;
			$res['qbdz'] = $qianbao ; //成功
			return $res;
		}
		else{
			$res['error'] = 1 ;
			$res['msg'] = '生成钱包地址出错！'; 
			return $res;
		}
	}
	else{
		$res['error'] = 1 ;
		$res['msg'] = '钱包链接失败！'; //如果获取失败,返回错误
		return $res;
	}
}
	
/* 提现 */
function sendtrance( $username = null, $to = null, $amount = null,$sxf = null)
{
	/*$dj_username = 'litecoin';
	$dj_password = '123456';
	$dj_address = '192.168.0.166';
	$dj_port = '40001';*/
	$dj_username = M("Ltset")->where(array('id'=>1))->getField("lt_username");
	$dj_password = M("Ltset")->where(array('id'=>1))->getField("lt_password");
	$dj_address = M("Ltset")->where(array('id'=>1))->getField("lt_address");
	$dj_port =  M("Ltset")->where(array('id'=>1))->getField("lt_port");
	$BtcClient = CoinClient($dj_username, $dj_password, $dj_address, $dj_port, 5, array(), 1);

	//链接钱包
	$json = $BtcClient->getinfo();//获取钱包信息

	if(isset($json['version']) && !empty($json['version'])) {
		$addr = $BtcClient->validateaddress($to); //验证对方地址
		if (!$addr['isvalid']) {
			$res['error'] = 1 ;
			$res['msg'] = '转出地址无效!'; 
			return $res;
		}
		
		if ($json['balance'] < $amount) {//获取钱包余额
			$res['error'] = 1 ;
			$res['msg'] = '钱包余额不足!'; 
			return $res;
		}

		$realamount = $amount - $sxf;
		$sendrs = $BtcClient->sendtoaddress($to, floatval($realamount));//发送交易

		$coin = 'ltc';
		if ($sendrs) {
			$flag = 1;
			$arr = json_decode($sendrs, true);
			if (isset($arr['status']) && ($arr['status'] == 0)) {//如果返回错误信息,则失败

				$flag = 0;
				$res['error'] = 1 ;
				$res['msg'] = '发送交易失败!'; 
				return $res;
				
			}else{
				$wlsxf = M("Sys")->where(array("name"=>"wlsxf"))->getField("value");
				$rs2  = M('myzc')->add(array('username' => $username, 'qbdz' => $to, 'coinname' => $coin, 'fee' => 0, 'wlsxf' => $wlsxf,'txid' => $sendrs, 'num' => $amount, 'mum' => $amount, 'addtime' => time(), 'status' => 1));
				if($rs2)
				{
					$usermoney = M('User')->where(array('username' => $username))->getField("usermoney");//先读取目前的余额
					$new_usermoney = $usermoney - $amount; //新的余额
					M('User')->where(array('username' => $username))->save(array('usermoney' => $new_usermoney));
					//插入记录
					$userid = M("User")->where(array("username"=>$username))->getField("id");
					M('Log')->add(array('user_id' => $userid, 'money' => -$amount, 'type' => 11, 'qbdz' => $to,'msg' => "提现：-$amount",'status'=>1, 'date' => time()));
					M('Log')->add(array('user_id' => $userid, 'money' => -$sxf, 'type' => 12, 'msg' => "提现手续费：-$sxf", 'date' => time()));
					$res['error'] = 0 ;
					return $res;
				}
				else
				{
					$res['error'] = 1 ;
					$res['msg'] = '保存提现记录出错!'; 
					return $res;
				}
			}
		} else {
			$res['error'] = 1 ;
			$res['msg'] = '发送交易失败!'; 
			return $res;
		}

	} else {
		$res['error'] = 1 ;
		$res['msg'] = '钱包链接失败！'; //如果获取失败,返回错误
		return $res;
	}

}
	
/* 交易记录(充值转入)计划任务 */
function ltcjob()
{
	//获取ltc交易记录的计划任务,需要设置计划任务,每1分钟执行一次,未执行完成不要重复执行
	//说明:$trans['amount']为用户转入LTC的数量;$trans['account']为创建用户时传入的$username;$trans['txid']为订单交易txid;
	//$trans['confirmations']为该笔交易的区块确认次数;$trans['address']为LTC公钥地址
	header("Content-type: text/html; charset=utf-8");
	set_time_limit(0);
	ignore_user_abort(true);

	/*$dj_username = 'litecoin';
	$dj_password = '123456';
	$dj_address = '192.168.0.166';
	$dj_port = '40001';*/

	$dj_username = M("Ltset")->where(array('id'=>1))->getField("lt_username");
	$dj_password = M("Ltset")->where(array('id'=>1))->getField("lt_password");
	$dj_address = M("Ltset")->where(array('id'=>1))->getField("lt_address");
	$dj_port =  M("Ltset")->where(array('id'=>1))->getField("lt_port");

	$CoinClient = CoinClient($dj_username, $dj_password, $dj_address, $dj_port, 5, array(), 1);
	//链接钱包

	$json = $CoinClient->getinfo();//获取钱包信息

	if (!isset($json['version']) || !$json['version']) {

		/*file_put_contents('ltclog.php', "<?php\r\nreturn " . var_export('钱包链接失败！----'.date("Y/m/d H:i:s"), true) . "?>", FILE_APPEND);*/
		/*echo '钱包链接失败！' . "<br>\n";*/
		exit;
	}

	$listtransactions = $CoinClient->listtransactions('*', 100, 0);//获取最近的100条交易记录
	/*echo '交易记录个数:' . count($listtransactions) . "<br>\n";*/


	if ($listtransactions != "nodata") {//如果获取到了记录,则继续
		krsort($listtransactions);
		foreach ($listtransactions as $key => $trans) {
			if($trans['timereceived'] > 1542251048){
		/*	echo 'aaa:'.$key.'<br/>';*/
			if (!$trans['account']) {
				/*file_put_contents('ltclog.php', "<?php\r\nreturn " . var_export('用户为空，继续下一层！----'.date("Y/m/d H:i:s"), true) . "?>", FILE_APPEND);*/
				/*echo '用户为空，继续下一层' . "<br><br>\n";*/
				continue;
			}

			if (!($user = M('User')->where(array('username' => $trans['account']))->find())) {
				//这里的if里面根据自己的系统用户表,写逻辑.逻辑为:查询 $trans['account']对应的用户名是否在系统中存在. $trans['account']为上面创建用户时传入的$username.

			/*	echo '用户不存在，继续下一层' . "<br><br>\n";*/
				/*file_put_contents('ltclog.php', "<?php\r\nreturn " . var_export('用户不存在，继续下一层！----'.date("Y/m/d H:i:s"), true) . "?>", FILE_APPEND);*/
				continue;
			}

			if (M('myzr')->where(array('txid' => $trans['txid'],'qbdz' => $trans['address'], 'status' => '1'))->find()) {
				//根据查询到的交易txid到用户转入表中查询该数据,如果查询到改数据,并且状态为1,则是已经转入成功的记录
				/*file_put_contents('ltclog.php', "<?php\r\nreturn " . var_export('交易已经完成了，继续下一层！----'.date("Y/m/d H:i:s"), true) . "?>", FILE_APPEND);*/
			/*	echo '交易已经完成了，继续下一层' . "<br><br>\n";*/
				continue;
			}

			/*echo 'all check ok ' . "<br>\n";*/
			
			//处理 充值转入 的交易记录
			if ($trans['category'] == 'receive') {
			/*	echo 'start receive do:' . "<br>\n";*/
				$coin        = 'ltc';
				$sfee        = 0;
				$true_amount = $trans['amount'];
				
				//区块确认<3 ,不能确定交易是否完成
				
				if ($trans['confirmations'] < 3) {
					
					/*echo ' 用户名称： '. $trans['account'] . ' ，confirmations： ' . $trans['confirmations'] . ' not elengh 3 continue ' . "<br>\n";*/

					if ($res = M('myzr')->where(array('txid' => $trans['txid'],'qbdz' => $trans['address']))->find()) {
						M('myzr')->save(array('id' => $res['id'], 'addtime' => time(), 'status' => intval($trans['confirmations'] - 3)));
						/*file_put_contents('ltclog.php', "<?php\r\nreturn " . var_export('用户不存在，继续下一层！----'.date("Y/m/d H:i:s"), true) . "?>", FILE_APPEND);*/
					} else {
						//区块确认<3,只插入充值转入表，方便下次直接判断
						$rs1 = M('myzr')->add(array('username' => $trans['account'], 'qbdz' => $trans['address'], 'coinname' => $coin, 'fee' => $sfee, 'txid' => $trans['txid'], 'num' => $true_amount, 'mum' => $trans['amount'], 'addtime' => time(), 'status' => intval($trans['confirmations'] - 3)));
						/*file_put_contents('ltclog.php', "<?php\r\nreturn " . var_export('用户不存在，继续下一层！----'.date("Y/m/d H:i:s"), true) . "?>", FILE_APPEND);*/
					/*	echo '区块确认<3，不做处理，只插入记录表' . "<br><br>\n";*/
					}

				} else {
					//区块确认>=3,才能确定一笔交易完成了
					/*echo '确定交易已经完成' . "<br>\n";*/
					/*file_put_contents('ltclog.php', "<?php\r\nreturn " . var_export('确定交易已经完成！'.date("Y/m/d H:i:s"), true) . "?>", FILE_APPEND);*/
					if ($res = M('myzr')->where(array('txid' => $trans['txid'],'qbdz' => $trans['address']))->find()) {
					/*	echo '设置这条充值转入记录,状态为1'. "<br>\n";*/

						$rs1 = M('myzr')->save(array('id' => $res['id'], 'addtime' => time(), 'status' => 1));


					} else {
					/*	echo '是一条新的充值转入记录，插入到数据库里' . "<br>\n";*/

						$rs1 = M('myzr')->add(array('username' => $trans['account'], 'qbdz' => $trans['address'], 'coinname' => $coin, 'fee' => $sfee, 'txid' => $trans['txid'], 'num' => $true_amount, 'mum' => $trans['amount'], 'addtime' => time(), 'status' => 1));
					}
					//写入转入成功记录,后面加上自己的逻辑,比如给该用户money字段加上相应的$trans['amount'].
					if($rs1){

						$usermoney = M('User')->where(array('username' => $trans['account']))->getField("usermoney");//先读取目前的余额
						$new_usermoney = $usermoney + $trans['amount']; //新的余额
						$rs = M('User')->where(array('username' => $trans['account']))->save(array('usermoney' => $new_usermoney));
						if (!$rs) {
							/*file_put_contents('ltclog.php', "<?php\r\nreturn " . var_export('平台充值出错！'.date("Y/m/d H:i:s"), true) . "?>", FILE_APPEND);*/
						/*	echo '平台充值出错！' . "<br><br>\n";*/
						}
						else{
							//插入记录
							$userid = M("User")->where(array("username"=>$trans['account']))->getField("id");
							 M('Log')->add(array('user_id' => $userid, 'money' => $trans['amount'], 'qbdz'=>$trans['address'] , 'type' => 10, 'msg' => "充值：".$trans['amount'], 'status' => 1, 'date' => time()));
							/*file_put_contents('ltclog.php', "<?php\r\nreturn " . var_export('平台充值成功！'.date("Y/m/d H:i:s"), true) . "?>", FILE_APPEND);*/
							/*echo '平台充值成功！' . "<br><br>\n";*/
						}
					}
				}
			
			}
			
			//处理 提现转出 的交易记录
			//交易失败的confirmations为-1
			if($trans['category'] == 'send' && $trans['confirmations'] == -1){
				$usermoney = M('User')->where(array('username' => $trans['account']))->getField("usermoney");//先读取目前的余额
				$new_usermoney = $usermoney - $trans['amount']; //新的余额
				$rs = M('User')->where(array('username' => $trans['account']))->save(array('usermoney' => $new_usermoney));
				if (!$rs) {
					/*file_put_contents('ltclog.php', "<?php\r\nreturn " . var_export('提现失败，增加系统余额出错！---- '.date("Y/m/d H:i:s"), true) . "?>", FILE_APPEND);*/
					/*echo '提现失败，增加系统余额出错！' . "<br><br>\n";*/
				}
				else{
					/*file_put_contents('ltclog.php', "<?php\r\nreturn " . var_export('提现成功，增加系统余额成功！----'.date("Y/m/d H:i:s"), true) . "?>", FILE_APPEND);*/
					/*echo '提现成功，增加系统余额成功！' . "<br><br>\n";*/
					//到时候看 是否可以获取到 哪条提现记录，改变其状态
				}
			}
			
		}
		}
	}
}
/* mai end 对接莱特币接口 */


//YPP 新增 添加矿机
function ypp_addkj($userId,$kj_id,$price,$leixing=0){
	$time = time();
	$daytime = strtotime(date("Y-m-d"),time()) + 86399;
	$kj = M("Kuangji")->where(array("id"=>$kj_id))->select();
	$endtime = $kj[0]['cycle']*86400 + $daytime;

	$data['user_id'] = $userId;
	$data['kj_id'] = $kj_id;
	$data['price'] = $price;
	$data['lilv'] = $kj[0]['lilv'];
	$data['type'] = $kj[0]['type'];
	$data['addtime'] = $time;
	$data['endtime'] = $endtime;
	$data['leixing'] = $leixing;
	$res = M("Userkj")->add($data);
    if($res){
		$user = M("User")->where(array("id"=>$userId))->find();
		$userhuoyue = $user['huoyue'];
		//基础活跃度比例
		$huoyue_jl = M("Sys")->where(array("name"=>"huoyue_jl"))->getField('value');
		$add_huoyue = $kj[0]['price']*$huoyue_jl*0.01;
		$huoyue = $userhuoyue + $add_huoyue;

		M("User")->where(array("id"=>$userId))->save(array('huoyue'=>$huoyue));
		add_log($userId,0,0,27, "获得矿机-'".$kj[0]['name']."'，奖励基础活跃度".$add_huoyue,$add_huoyue);
		//推荐人获得加成活跃度
		if($user['parent'] > 0){
			$parent =  M("User")->where(array("id"=>$user['parent']))->find();
			$huoyue_jc = M("Sys")->where(array("name"=>"huoyue_jc"))->getField('value');
			$huoyue_jc = $add_huoyue*$huoyue_jc*0.01;
			$jchuoyue = $parent['huoyue_jc'] + $huoyue_jc;
			M("User")->where(array("id"=>$user['parent']))->save(array('huoyue_jc'=>$jchuoyue));
			add_log($user['parent'],0,0,28, "用户'".$user['username']."'获得基础活跃度".$kj[0]['huoyue']."，奖励加成活跃度".$huoyue_jc,$huoyue_jc);
			//星级导师检测升级
			upgradeuser($user['parent']);
		}

		if($kj[0]['type'] == 1){
			$dt['now_num'] = $kj[0]['now_num'] + 1;
		}else{
			$dt['now_money'] = $kj[0]['now_money'] + $price;
		}
		M("Kuangji")->where(array("id"=>$kj_id))->save($dt);//矿机已售出数量+1
		return "true";
	}else{
		return "false";
	}

}

//星级导师检测升级
function upgradeuser($userid){
	while($userid >0){
		$user = M("User")->where(array("id"=>$userid,"shiming"=>1))->find();
		//获取用户活跃度信息
		$data = checkuser($userid);
		$userclass = M("Userclass")->order("id asc")->select();
		foreach($userclass as $k=>$v){
			if($v['need_num'] > $data['ztrs'] || $v['team_huoyue'] >  $data['tdhyd'] || $v['youxiao_huoyue'] > $data['yxhyd']){
				$class = $v['class']-1;
				$kjjl = $v['kjjl'];
				break;
			}
		}
		M('User')->where(array('id' => $userid))->save(array('user_class' => $class));
		//星级导师赠送矿机
		$xingjikj = M("Userkj")->where(array("user_id"=>$userid,'kj_id'=>$kjjl,'leixing'=>1))->find();
		if($class > 0 && !$xingjikj){
			$kj = M("Kuangji")->where(array("id"=>$kjjl))->find();
			ypp_addkj($userid,$kjjl,$kj['price'],1);
		}
		$userid = $user['parent']?$user['parent']:0;
	}
}

function checkuser($userid){
	//直推人数
	$ztrs = 0;
	$ztuser = M("User")->where(array("parent"=>$userid,"shiming"=>1))->select();
	foreach($ztuser as $k=>$v){
		$haskj = M("Userkj")->where(array("user_id"=>$v['id'],"status"=>0))->find();
		if($haskj){
			$ztrs++;
		}
	}

	//团队活跃度
	$users = M("User")->where(array("shiming"=>1))->select();
	//团队所有会员
	$childs = GetTeamMember($users,$userid);
	$childs = implode(',',$childs);
	$childs = substr($childs,0,strlen($childs)-1);
	if($childs){
		$jichu = M("User")->where(array("id"=>array("in",$childs)))->sum('huoyue');//基础活跃度
		$jichu = $jichu?$jichu:0;
		$jiacheng = M("User")->where(array("id"=>array("in",$childs)))->sum('huoyue_jc');//加成活跃度
		$jiacheng = $jiacheng?$jiacheng:0;
		$tdhyd = $jichu + $jiacheng;//团队活跃度 = 基础活跃度+加成活跃度
	}


	//有效活跃度
	$tdhyds = array();
	$teams = M("User")->where(array("parent"=>$userid,"shiming"=>1))->select();
	if($teams){
		foreach($teams as $k=>$v){
			$childs1 = GetTeamMember($users,$v['id']);
			$childs1 = implode(',',$childs1);
			$childs1 = substr($childs1,0,strlen($childs1)-1);
			if($childs1){
				$jichu1 = M("User")->where(array("id"=>array("in",$childs1)))->sum('huoyue');//基础活跃度
				$jichu1 = $jichu1?$jichu1:0;
				$jiacheng1 = M("User")->where(array("id"=>array("in",$childs1)))->sum('huoyue_jc');//加成活跃度
				$jiacheng1 = $jiacheng1?$jiacheng1:0;
				$tdhyds[] = $jichu1 + $jiacheng1;//团队活跃度 = 基础活跃度+加成活跃度
			}
		}
		//有效活跃度 = 去掉团队内 团队活跃度最高的两个，然后求和。
		arsort($tdhyds);
		$arrs = array_values($tdhyds);
		for ($i = 0; $i < count($arrs); $i++) {
			if($i<2){ //
				unset($arrs[$i]);
			}
		}
		$yxhyd = array_sum($arrs);
	}

	return array('ztrs'=>$ztrs,'tdhyd'=>$tdhyd?$tdhyd:0,'yxhyd'=>$yxhyd?$yxhyd:0);
}

function GetTeamMember($members, $mid) {
	$Teams=array();//最终结果
	$mids=array($mid);//第一次执行时候的用户id
	do {
		$othermids=array();
		$state=false;
		foreach ($mids as $valueone) {
			foreach ($members as $key => $valuetwo) {
				if($valuetwo['parent']==$valueone){
					$Teams[]=$valuetwo['id'];//找到我的下级立即添加到最终结果中
					$othermids[]=$valuetwo['id'];//将我的下级id保存起来用来下轮循环他的下级
					array_splice($members,$key,1);//从所有会员中删除他
					$state=true;
				}
			}
		}
		$mids=$othermids;//foreach中找到的我的下级集合,用来下次循环
	} while ($state==true);
	return $Teams;
}









